﻿using System.Linq.Expressions;
using AutoMapper;
using CozeDomain.Entities;
using CozeDomain.IRepositories;
using CozeDomain.IServices;
using D.Util.UtilCache;
using D.Util.UtilConfiguration;
using D.UtilCore.Dtos;
using D.UtilCore.JwtUtil;
using D.UtilCore.Models;
using D.UtilCore.Utils;
using D.UtilFreesqlOrm;
using Microsoft.AspNetCore.Http;
using System.Data;
using D.UtilCore.WChartUtils;
using D.Util.UtilNLog;
using CozeDto.SaleHistoryDtos;
using CozeDto.SaleApplyForDtos;

namespace CozeDomain.Services
{
    /// <summary>
    /// 管理员业务实现
    /// </summary>
    public class SaleHistoryService : BaseService<SaleHistoryEntity>, ISaleHistoryService
    {
        private readonly IMapper _mapper;
        private readonly ICacheService _cache;
        private readonly IJwtService _tokenService;
        private readonly INLogService _nlogService;
        private readonly ISaleHistoryEntityRepository _repository;
        private readonly IHttpContextAccessor _httpContextAccessor;

        /// <summary>
        /// 构造注入
        /// </summary>
        public SaleHistoryService(IMapper mapper, ICacheService cache, IJwtService tokenService, INLogService nlogService, IHttpContextAccessor httpContextAccessor,
            ISaleHistoryEntityRepository repository)
        {
            // 用于base方法
            base._baseDal = repository;
            this._repository = repository;
            _cache = cache;
            _mapper = mapper;
            _nlogService = nlogService;
            _tokenService = tokenService;
            _httpContextAccessor = httpContextAccessor;
        }

        #region 增删改查

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> AddSaleHistoryAsync(ReqAddSaleHistory reqModel)
        {

            var insertEntity = _mapper.Map<ReqAddSaleHistory, SaleHistoryEntity>(reqModel);
            insertEntity.DeleteFlag = false;
            insertEntity.CreateTime = DateTime.Now;
            insertEntity.Id = D.UtilFreesqlOrm.FreesqlOrm.NewId;

            var dbres = await _repository.AddAsync(insertEntity);

            return dbres.NormalResponse();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> DeleteSaleHistoryAsync(List<string> ids)
        {
            var dbres = await _repository.DeleteAsync(m => ids.Contains(m.Id));
            return dbres.NormalResponse();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdateSaleHistoryAsync(ReqUpdateSaleHistory reqModel)
        {
            var updateEntity = await _repository.GetModelAsync(m => m.Id == reqModel.Id);
            if (updateEntity == null)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, Msg = "该信息不存在" };
            }

            updateEntity.UpdateTime = DateTime.Now;
            updateEntity.UpdateUser = reqModel.UpdateUser;

            var dbres = await _repository.UpdateAsync(updateEntity);

            return dbres.NormalResponse();
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApiResult<RepSaleHistoryInfo>> GetSaleHistoryAsync(string id)
        {
            var dbres = await _repository.GetModelAsync(m => m.Id == id);
            var res = _mapper.Map<SaleHistoryEntity, RepSaleHistoryInfo>(dbres);
            return res.GetResponse();
        }

        /// <summary>
        /// 获取分页
        /// </summary>
        /// <param name="reqQuerySaleHistoryPage"></param>
        /// <returns></returns>
        public async Task<ApiResult<PageModel<RepSaleHistoryInfo>>> QuerySaleHistoryPagesAsync(ReqQuerySaleHistoryPage reqQuerySaleHistoryPage)
        {
            var dbres = await _repository.QuerySaleHistoryPagesAsync(reqQuerySaleHistoryPage);
            var res = new PageModel<RepSaleHistoryInfo>()
            {
                PageNo = reqQuerySaleHistoryPage.PageNo,
                PageSize = reqQuerySaleHistoryPage.PageSize,
                Data = dbres.Item2,
                TotalCount = dbres.Item1,
                TotalPage = reqQuerySaleHistoryPage.PageSize > 0 ? (int)Math.Ceiling(dbres.Item1 / (double)reqQuerySaleHistoryPage.PageSize) : 0
            };
            return res.GetResponse();
        }
        #endregion
    }
}